<template xmlns:v-slot="http://www.w3.org/1999/XSL/Transform">
  <wx-tables
    :columns="columns"
    title="问题反馈"
    :data-source="dataSource"
    :loading="loading"
    :total="total"
    :size.sync="size"
    :page.sync="page"
    @handlePage="handlePage"
  >
    <!-- tools -->
    <template #tools>
<!--      <el-button type="primary" class="table-btn" @click="handleOpenForm()">新增</el-button>-->
      <!--      <el-button class="table-btn" icon="el-icon-refresh-right" @click="handleGetTable">刷新</el-button>-->
    </template>
    <template v-slot:img_arr="record">
      <span v-if="!record.row.img_arr || !record.row.img_arr.length">-</span>
      <el-avatar v-else shape="square" :size="100" fit="cover" :src="record.row.img_arr[0]"></el-avatar>
    </template>
    <template v-slot:content="record">
      <div style="overflow: hidden;text-overflow: ellipsis;white-space: nowrap;">
        <span> {{record.row.content}} </span>
      </div>
    </template>
    <template v-slot:tel="record">
      <div style="overflow: hidden;text-overflow: ellipsis;white-space: nowrap;">
        <span> {{record.row.tel ? record.row.tel : '-' }} </span>
      </div>
    </template>
    <template v-slot:answer="record">
      {{ record.row.answer ? '已回复' : '未回复' }}
    </template>
    <template v-slot:oper="record">
      <div class="oper-box">
        <span><el-link type="primary" @click="handleEit(record.row)">回复</el-link></span>
<!--        <span><el-link type="primary" @click="handleChangePassWord(record.row.id)">重置密码</el-link></span>-->
<!--        <el-popconfirm-->
<!--          title="确定删除当前账号吗？"-->
<!--          @confirm="handleDelete(record.row.id)"-->
<!--        >-->
<!--          <el-link slot="reference" type="danger">删除</el-link>-->
<!--        </el-popconfirm>-->
      </div>
    </template>
    <wx-dialog
      ref="wx-dialog"
      :visible.sync="formVisible"
      :is-add="isAdd"
      width="800px"
      @ok="handleSaveForm"
    >
      <div class="wx-dialog-form">
        <el-form ref="ruleForm" v-loading="formLoading" :rules="ruleForm" :model="formInfo" label-position="left" label-width="90px">
          <el-form-item label="反馈内容">
            <span>{{formInfo.content}}</span>
          </el-form-item>
          <el-form-item label="图片">
            <span v-if="!formInfo.img_arr || !formInfo.img_arr.length">暂无</span>
            <el-avatar v-else v-for="item in formInfo.img_arr" :key="item"
                       style="margin-right: 12px;"
                       shape="square" :size="100" fit="cover" :src="item"></el-avatar>
          </el-form-item>
          <el-form-item label="联系电话">
            <span>{{formInfo.tel ? formInfo.tel : '暂无'}}</span>
          </el-form-item>
          <el-form-item label="回复" prop="answer">
            <Tinymce v-model="formInfo.answer"  :height="300"/>
          </el-form-item>
        </el-form>
      </div>
    </wx-dialog>
  </wx-tables>
</template>

<script>
import WxDialog from '@/components/WxDialog'
import WxTables from '@/components/WxTables'
import TableMixins from '@/components/WxTables/mixins'
import { sysNewsCreate, sysNewsGetListByCon } from '@/api/problem'
import Tinymce from '@/components/Tinymce/Tinymce'

export default {
  name: 'ProblemFeedback',
  components: {
    WxDialog,
    WxTables,
    Tinymce
  },
  mixins: [TableMixins],
  data() {
    return {
      pwdDialog: false,
      columns: [
        { config: { type: 'index', width: 100 }, label: '序号' },
        { dataIndex: 'img_arr', label: '图片', slots: true },
        { dataIndex: 'content', label: '反馈内容', slots: true },
        { dataIndex: 'tel', label: '联系电话', slots: true },
        { dataIndex: 'answer', label: '回复状态', slots: true },
        { dataIndex: 'oper', label: '操作', slots: true }
      ],
      markId: '',
      town_id: '',
      ruleForm: {
        answer: [
          { required: true, message: '请输入内容', trigger: 'blur' }
        ]
      },
      formInfo: {}
    }
  },
  methods: {
    /**
     * 获取列表
     */
    getList() {
      sysNewsGetListByCon({town_id:this.town_id,page: this.page}).then(res => {
        if (this.pagination) {
          const { data, total } = res.data
          this.total = total
          this.dataSource = data
          this.cacheDataSource = JSON.parse(JSON.stringify(data))
        } else {
          this.dataSource = res.data
          this.cacheDataSource = JSON.parse(JSON.stringify(res.data))
        }
        this.handleSetCancelStatus?.()
        // this.handleCompleteTable()
      }).finally(() => {
        this.loading = false
      })
    },
    /**
     * 新增/编辑人员
     */
    createManager(idSend) {
      if(Object.keys(idSend).length) {
        sysNewsCreate({ ...this.formInfo,...idSend,town_id: this.town_id })
          .then((res) => {
            if (res.code === 200) {
              this.$message.success(this.isAdd ? '添加成功' : '编辑成功')
              this.formInfo = {}
              this.formVisible = false
              this.handleGetTable()
            }
          }).finally(() => {
          this.handleResetDialog()
        })
      }else{
        sysServiceCreate({ ...this.formInfo,town_id: this.town_id })
          .then((res) => {
            if (res.code === 200) {
              this.$message.success(this.isAdd ? '添加成功' : '编辑成功')
              this.formInfo = {}
              this.formVisible = false
              this.handleGetTable()
            }
          }).finally(() => {
          this.handleResetDialog()
        })
      }
    },
    /**
     * 更改人员状态
     */
    changeStatus(id,status) {
      managerSetStatus({
        admin_town_id: id,
        status
      }).then((res) => {
        if (res.code === 200) {
          this.$message.success(res.msg)
          this.handleGetTable()
        }
      })
    },
    /**
     * 重置人员密码
     * @param id
     */
    handleChangePassWord(id) {
      adminPortalResetPassword({ id,type:2 }).then((res) => { if (res.code === 200) { this.$message.success(res.msg) } })
    },
    /**
     * 编辑
     * @param item
     */
    handleEit(item) {
      if(item.answer) return this.$message.warning('不可再次编辑')
      const { id, content, answer, tel, img_arr } = item
      this.markId = 'sys_question_id'
      this.handleOpenForm(id,['content','answer','tel', 'img_arr'],{content,answer,tel,img_arr})
    },
    /**
     * 删除管理员
     * @param admin_ids
     */
    handleDelete(admin_ids) {
      managerDestroy({ admin_ids }).then((res) => { if (res.code === 200) {
        this.$message.success(res.msg)
        this.handleGetTable()
      } })
    },
    /**
     * 翻页
     * @param val
     */
    handlePage(val) {
      this.page = val
    }
  }
}
</script>
<style lang="scss" src="./style/index.scss" scoped/>
<style scoped>
::v-deep .el-avatar>img {
  width: 100%;
}
</style>
